package de.revdev.llt.bean.facade;

import de.revdev.llt.bean.entities.FileCardBox;
import de.revdev.llt.bean.entities.FileCardBox_;
import de.revdev.llt.bean.entities.Participants;
import de.revdev.llt.bean.entities.Participants_;
import de.revdev.llt.bean.entities.PublicClassRoom;
import de.revdev.llt.bean.entities.PublicClassRoom_;
import de.revdev.llt.bean.entities.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

/**
 *
 * @author f.lindermaier
 */
@Stateless
public class PublicClassRoomFacade extends AbstractFacade<PublicClassRoom> {

    @PersistenceContext(unitName = "LetsLearnTogetherPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public PublicClassRoomFacade() {
        super(PublicClassRoom.class);
    }

    public List<PublicClassRoom> getForeignPublicClassRooms(User user, Boolean accepted) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<PublicClassRoom> cq = cb.createQuery(PublicClassRoom.class);
        Root<PublicClassRoom> r = cq.from(PublicClassRoom.class);
        Join<PublicClassRoom, Participants> j = r.join(PublicClassRoom_.participants);
        cq.select(r);
        ParameterExpression<User> p = cb.parameter(User.class);
        ParameterExpression<Boolean> q = cb.parameter(Boolean.class);
        cq.where(cb.equal(j.get(Participants_.user), p), cb.equal(j.get(Participants_.accepted), q));
        return em.createQuery(cq).setParameter(p, user).setParameter(q, accepted).getResultList();
    }

    public List<PublicClassRoom> getOwnPublicClassRooms(User user) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<PublicClassRoom> cq = cb.createQuery(PublicClassRoom.class);
        Root<PublicClassRoom> r = cq.from(PublicClassRoom.class);
        cq.select(r);
        ParameterExpression<User> p = cb.parameter(User.class);
        cq.where(cb.equal(r.get(PublicClassRoom_.owner), p));
        return em.createQuery(cq).setParameter(p, user).getResultList();
    }

    public PublicClassRoom getFCBforPuCR(PublicClassRoom publicClassRoom) {
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<PublicClassRoom> cq = cb.createQuery(PublicClassRoom.class);
            Root<PublicClassRoom> r = cq.from(PublicClassRoom.class);
            Fetch<PublicClassRoom, FileCardBox> f = r.fetch(PublicClassRoom_.fileCardBoxes);
            cq.select(r);
            ParameterExpression<Long> p = cb.parameter(Long.class);
            cq.where(cb.equal(r.get(PublicClassRoom_.id), p));
            return em.createQuery(cq).setParameter(p, publicClassRoom.getId()).getSingleResult();
        } catch (Exception e) {
            return publicClassRoom;
        }
    }
}
